package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Employee;
import com.sky.mapper.CateGoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CateGoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CateGoryServiceImpl implements CateGoryService {
    @Autowired
    private CateGoryMapper cateGoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

//套餐分页
    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        if (categoryPageQueryDTO.getType()==null){
            int count = cateGoryMapper.selectCount();
            int start,end;
            start = (categoryPageQueryDTO.getPage()-1) * categoryPageQueryDTO.getPageSize() < 0 ? 0 :
                    (categoryPageQueryDTO.getPage()-1) * categoryPageQueryDTO.getPageSize();
            System.out.println((categoryPageQueryDTO.getPage()-1) * categoryPageQueryDTO.getPageSize());
            end = categoryPageQueryDTO.getPage() * categoryPageQueryDTO.getPageSize() <= count ?
                    categoryPageQueryDTO.getPageSize() :
                    (count - (categoryPageQueryDTO.getPage()-1) * categoryPageQueryDTO.getPageSize() < 0 ? count :
                            count - (categoryPageQueryDTO.getPage()-1) * categoryPageQueryDTO.getPageSize());
            List<Category> records = cateGoryMapper.select(start,end);
            return new PageResult(count,records);
        }else{
            int count = cateGoryMapper.selectNameCount(categoryPageQueryDTO.getName(),categoryPageQueryDTO.getType());
            int start,end;
            start = (categoryPageQueryDTO.getPage()-1) * categoryPageQueryDTO.getPageSize() < 0 ? 0 :
                    (categoryPageQueryDTO.getPage()-1) * categoryPageQueryDTO.getPageSize();
            System.out.println((categoryPageQueryDTO.getPage()-1) * categoryPageQueryDTO.getPageSize());
            end = categoryPageQueryDTO.getPage() * categoryPageQueryDTO.getPageSize() <= count ?
                    categoryPageQueryDTO.getPageSize() :
                    (count - (categoryPageQueryDTO.getPage()-1) * categoryPageQueryDTO.getPageSize() < 0 ? count :
                            count - (categoryPageQueryDTO.getPage()-1) * categoryPageQueryDTO.getPageSize());
            List<Category> records = cateGoryMapper.selectName(categoryPageQueryDTO.getName(),categoryPageQueryDTO.getType(),start,end);
            return new PageResult(count,records);
        }

    }
//新增分类
    @Override
    public void add(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        cateGoryMapper.add(category);
    }
//修改分类
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        cateGoryMapper.update(category);
    }
//状态启停
    @Override
    public void startOrStop(Integer status, Long id) {
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        cateGoryMapper.update(category);
    }
//根据id删除分类
    @Override
    public int delete(Long id) {
        if (dishMapper.selectCate(id)>0 || setmealMapper.selectCate(id)>0){
            return 0;
        }
        cateGoryMapper.delete(id);
        return 1;
    }
//根据类型查询分类
    @Override
    public List<Category> list(Integer type) {
        return cateGoryMapper.list(type);
    }


}
